﻿using AutoMapper;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using BW.MesProject.WareHouse.Domain;
using BW.MesProject.WareHouse.Infrastructure;
using BW.MesProject.WareHouse.Infrastructure.Dto.Input;
using BW.MesProject.WareHouse.Infrastructure.Dto.OutPut;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BW.MesProject.WareHouse.Application
{
    /// <summary>
    /// 标签打印
    /// </summary>
    public class LabelPrintServices:ILabelPrintServices
    {
        /// <summary>
        /// 标签打印
        /// </summary>
        private readonly IBaseRepository<LabelPrintModel> _LabelPrint;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="LabelPrint"></param>
        /// <param name="mapper"></param>
        public LabelPrintServices(IBaseRepository<LabelPrintModel> LabelPrint, IMapper mapper)
        {
            _LabelPrint = LabelPrint;
            this._mapper = mapper;
        }

        /// <summary>
        /// 新增标签打印
        /// </summary>
        /// <param name="LabelModel">标签打印信息</param>
        /// <returns>操作结果</returns>
        public ApiResult<int> AddLabel(LabelPrintModel LabelModel)
        {
            try
            {
                ////判断标签打印编码，名称，供应商，解析规则是否为空
                //if (string.IsNullOrEmpty(LabelModel.LalId) || string.IsNullOrEmpty(LabelModel.LabName) || string.IsNullOrEmpty(LabelModel.SupplierId.ToString()) || string.IsNullOrEmpty(LabelModel.Format.ToString()))
                //{
                //    return new ApiResult<int>()
                //    {
                //        Code = ApiEnums.Fail,
                //        Message = "请检查标签打印编码，名称，供应商，解析规则是否为空"
                //    };
                //}
                
                LabelModel.LPId = YitIdHelper.NextId();

                //添加
                var models = _mapper.Map<LabelPrintModel>(LabelModel);

                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "添加成功",
                    Data = _LabelPrint.Add(LabelModel),
                    TotalCount = 0
                };
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"添加标签打印时发生异常: {ex.Message}");

                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"添加失败: {ex.Message}",
                    Data = 0,
                    TotalCount = 0
                };
            }
        }

        /// <summary>
        /// 查询标签打印
        /// </summary>
        /// <param name="qryInput">查询传参</param>
        /// <returns></returns>
        public ApiResult<List<LabelPrintModel>> GetLabel(QryInput qryInput)
        {
            try
            {
                int totalCount = 0;

                // 获取所有标签打印
                var list = _LabelPrint.GetAll();
                List<LabelPrintModel> LPlist = new List<LabelPrintModel>();

                if (qryInput.PageSize == 0)
                {

                    list = list.OrderByDescending(u => u.CreatedTime);
                }
                else
                {
                    // 按编码查询
                    if (!string.IsNullOrEmpty(qryInput.QryID) && !string.IsNullOrEmpty(qryInput.QryName))
                    {
                        list = list.Where(x => x.Source.Contains(qryInput.QryID) && x.DeliverNum.Contains(qryInput.QryName));
                    }
                    else if (!string.IsNullOrEmpty(qryInput.QryID))
                    {
                        list = list.Where(x => x.Source.Contains(qryInput.QryID));
                    }
                    else if (!string.IsNullOrEmpty(qryInput.QryName))
                    {
                        list = list.Where(x => x.DeliverNum.Contains(qryInput.QryName));
                    }

                    LPlist = list.OrderByDescending(u => u.CreatedTime).ToPageList(qryInput.PageNum, qryInput.PageSize, ref totalCount);

                }
                return new ApiResult<List<LabelPrintModel>>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = LPlist,
                    TotalCount = totalCount,
                    PageNum = qryInput.PageNum,
                    PageSize = qryInput.PageSize
                };

            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"查询标签打印时发生异常: {ex.Message}");

                return new ApiResult<List<LabelPrintModel>>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败: {ex.Message}",
                    Data = new List<LabelPrintModel>(),
                    TotalCount = 0
                };
            }
        }

    }
}
